package com.woniuxy.mapper.impl;

import com.woniuxy.mapper.StudentDao;
import com.woniuxy.entity.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者
 * @create 2025-08-04 11:34
 * 学生的数据操作接口--文件实现
 */
public class StudentDaoImpl implements StudentDao {

    static String path = "d:\\aaa\\students.txt";

    @Override
    public void insert(Student student) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(path, true))) {
            bw.write(student.getSid() + "-");
            bw.write(student.getSname() + "-");
            bw.write(student.getPassword() + "-");
            bw.write(student.getAge() + "-");
            bw.write(student.getEmail());
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectBySname(String sname) {
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[1].equals(sname)) {
                    return new Student(Integer.parseInt(properties[0]),
                            properties[1],
                            properties[2],
                            Integer.parseInt(properties[3]),
                            properties[4]);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectByEmail(String email) {
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[4].equals(email)) {
                    return new Student(Integer.parseInt(properties[0]),
                            properties[1],
                            properties[2],
                            Integer.parseInt(properties[3]),
                            properties[4]);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public int selectMaxSid() {
        if (!new File(path).exists())
            return 0;
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line = null;
            int sid = 0;
            while ((line = br.readLine()) != null) {
                String[] properties = line.split("-");
                sid = Integer.parseInt(properties[0]);
            }
            return sid;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Student> selectAll() {
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            List<Student> students = new ArrayList<>();
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] properties = line.split("-");
                Student student = new Student(Integer.parseInt(properties[0]),
                        properties[1],
                        properties[2],
                        Integer.parseInt(properties[3]),
                        properties[4]);
                students.add(student);
            }
            return students;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAll(List<Student> students) {
        try(BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {
            for (Student student : students) {
                bw.write(student.getSid() + "-");
                bw.write(student.getSname() + "-");
                bw.write(student.getPassword() + "-");
                bw.write(student.getAge() + "-");
                bw.write(student.getEmail());
                bw.newLine();
                bw.flush();
            }
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


}
